<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
    <link rel="icon" href="image/favicon.ico">

</head>
<body>
    <div class="nav">五子棋对战</div>
    <!--登录对话框-->
    <div class="login-container">
        <div class="login-dialog">
            <!--标题-->
            <h2>登录</h2>
            <!--输入账号-->
            <div class="row">
                <span>用户名:</span>
                <input type="text" id="userName">
            </div>
            <!--输入密码-->
            <div class="row">
                <span>密码:</span>
                <input type="password" id="password">
            </div>
            <!--     提交   -->
            <div class="row">
                <button id="submit" onclick="submit()">提交</button>  <!--点击后交给后端-->
            </div>

            <div class="row">
                <button id="register">去注册</button>
            </div>

        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        let register = document.querySelector("#register");
        register.onclick = function () {
            location.replace("/register.html");
        };
        /*将数据提交给后端*/
        function submit () {

            $.ajax({
                type: "post",
                url: "/user/login",
                data: {
                    userName: $("#userName").val(),
                    password: $("#password").val()
                },
                success: function (result) {
                    if (result != null && result.code == 200 && result.data != null) {
                        console.log("登陆成功!");
                        alert(result.message);
                        location.replace("game_hall.html");
                    }else {
                        alert(result.message);
                    }
                },
                error: function () {
                    alert("登录失败!");
                }
            });

        }
    </script>
</body>
</html>